/**
 * CHMsgProgress.mm
 * @author Nick Kreeger <nick.kreeger@park.edu>
 */

#include "CHMsgProgress.h"
#import <Foundation/Foundation.h>
#import "NSString+Utils.h"

NS_IMPL_ISUPPORTS3(CHMsgProgress, nsIMsgProgress, nsIWebProgressListener, nsIPrompt);

CHMsgProgress::CHMsgProgress()
{
  NSLog(@"Creating a CHMsgProgress()\n");
  mShouldCloseDialog = NO;
}

CHMsgProgress::~CHMsgProgress()
{
}

void 
CHMsgProgress::SetProgressDelegate(id<MessageSendListener> aProgressDelegate)
{
  mProgressDelgate = aProgressDelegate;
}

NS_IMETHODIMP 
CHMsgProgress::OpenProgressDialog(nsIDOMWindowInternal* parent, nsIMsgWindow* aMsgWindow, const char* dialogURL, nsISupports* parameters)
{
  NSLog(@"CHMsgProgress::OpenProgressDialog");
  return NS_OK;
}

NS_IMETHODIMP 
CHMsgProgress::CloseProgressDialog(PRBool forceClose)
{
  NSLog(@"CHMsgProgress::CloseProgressDialog");
  return NS_OK;
}

NS_IMETHODIMP 
CHMsgProgress::RegisterListener(nsIWebProgressListener* listener)
{
  NSLog(@"CHMsgProgress::RegisterListener");
  return NS_OK;
}

NS_IMETHODIMP 
CHMsgProgress::UnregisterListener(nsIWebProgressListener* listener)
{
  NSLog(@"CHMsgProgress::UnregisterListener");
  return NS_OK;
}

NS_IMETHODIMP 
CHMsgProgress::GetPrompter(nsIPrompt* *_retval)
{
  *_retval = this;
  
  NS_IF_ADDREF(*_retval);
  
  NSLog(@"CHMsgProgress::GetPrompter");
  return NS_OK;
}

NS_IMETHODIMP 
CHMsgProgress::GetProcessCanceledByUser(PRBool* aProcessCanceledByUser)
{
  return NS_OK;
}

NS_IMETHODIMP 
CHMsgProgress::SetProcessCanceledByUser(PRBool aProcessCanceledByUser)
{
  return NS_OK;
}

NS_IMETHODIMP 
CHMsgProgress::GetMsgWindow(nsIMsgWindow* * aMsgWindow)
{
  return NS_OK;
}

NS_IMETHODIMP 
CHMsgProgress::SetMsgWindow(nsIMsgWindow*  aMsgWindow)
{
  return NS_OK;
}

#pragma mark -

NS_IMETHODIMP
CHMsgProgress::OnStateChange(nsIWebProgress* aWebProgress, nsIRequest* aRequest, PRUint32 aStateFlags, nsresult aStatus)
{
  /*
  NSString* str = @"CHMsgProgress::OnStateChange()";
  switch (aStateFlags)
  {
    case nsIWebProgressListener::STATE_START:
      str = [str stringByAppendingString:@"STATE_START"];
      break;
    case nsIWebProgressListener::STATE_STOP:
      str = [str stringByAppendingString:@"STATE_STOP"];
      break;
    case nsIWebProgressListener::STATE_TRANSFERRING:
      str = [str stringByAppendingString:@"STATE_TRANSFERRING"];
      break;
  }
  
  NSLog(str);
   */
  
  if (aStateFlags == nsIWebProgressListener::STATE_START)
    [mProgressDelgate onStartSending];
  
  return NS_OK;
}

NS_IMETHODIMP 
CHMsgProgress::OnProgressChange(nsIWebProgress* aWebProgress, 
                                nsIRequest* aRequest, 
                                PRInt32 aCurSelfProgress, 
                                PRInt32 aMaxSelfProgress, 
                                PRInt32 aCurTotalProgress, 
                                PRInt32 aMaxTotalProgress)
{
  [mProgressDelgate onStopSending:nil];  // pass a message?
  return NS_OK;
}

NS_IMETHODIMP 
CHMsgProgress::OnLocationChange(nsIWebProgress* aWebProgress, nsIRequest* aRequest, nsIURI* aLocation)
{
  NSLog(@"CHMsgProgress:OnLocationChange()");
  return NS_OK;
}

NS_IMETHODIMP 
CHMsgProgress::OnStatusChange(nsIWebProgress* aWebProgress, nsIRequest* aRequest, nsresult aStatus, const PRUnichar* aMessage)
{
  NSString* str = [NSString stringWithFormat:@"CHMsgProgress::OnStatusChange() --> %@", [NSString stringWithPRUnichars:aMessage]];
  [mProgressDelgate onStatus:[NSString stringWithPRUnichars:aMessage]];
  NSLog(str);
  return NS_OK;
}

NS_IMETHODIMP
CHMsgProgress::OnSecurityChange(nsIWebProgress* aWebProgress, nsIRequest* aRequest, PRUint32 aState)
{
  return NS_OK;
}

#pragma mark -

NS_IMETHODIMP 
CHMsgProgress::Alert(const PRUnichar* dialogTitle, const PRUnichar* text)
{
  NSLog(@"Alert() -> %@, %@", [NSString stringWithPRUnichars:dialogTitle], [NSString stringWithPRUnichars:text]);
  return NS_OK;
}

NS_IMETHODIMP 
CHMsgProgress::AlertCheck(const PRUnichar* dialogTitle, const PRUnichar* text, const PRUnichar* checkMsg, PRBool* checkValue)
{
  NSLog(@"Alert() -> %@, %@, %@", [NSString stringWithPRUnichars:dialogTitle], [NSString stringWithPRUnichars:text],
        [NSString stringWithPRUnichars:checkMsg]);
  
  return NS_OK;
}

NS_IMETHODIMP 
CHMsgProgress::Confirm(const PRUnichar* dialogTitle, const PRUnichar* text, PRBool* _retval)
{
  NSLog(@"Confirm() %@, %@", [NSString stringWithPRUnichars:dialogTitle], [NSString stringWithPRUnichars:text]);
  return NS_OK;
}

NS_IMETHODIMP 
CHMsgProgress::ConfirmCheck(const PRUnichar* dialogTitle, const PRUnichar* text, 
                            const PRUnichar* checkMsg, PRBool* checkValue, PRBool* _retval)
{
  NSLog(@"ConfirmCheck() %@, %@, %@", [NSString stringWithPRUnichars:dialogTitle], [NSString stringWithPRUnichars:text],
        [NSString stringWithPRUnichars:checkMsg]);
  
  return NS_OK;
}

NS_IMETHODIMP 
CHMsgProgress::ConfirmEx(const PRUnichar* dialogTitle, const PRUnichar* text, PRUint32 buttonFlags,
                         const PRUnichar* button0Title, const PRUnichar* button1Title, 
                         const PRUnichar* button2Title, const PRUnichar* checkMsg, 
                         PRBool* checkValue, PRInt32* _retval)
{
  NSLog(@"ConfirmEx() ADD IF NEEDED, large...");
  return NS_OK;
}

NS_IMETHODIMP 
CHMsgProgress::Prompt(const PRUnichar* dialogTitle, const PRUnichar* text, PRUnichar* *value, 
                      const PRUnichar* checkMsg, PRBool* checkValue, PRBool* _retval)
{
  NSLog(@"Prompt() %@, %@, %@", [NSString stringWithPRUnichars:dialogTitle], [NSString stringWithPRUnichars:text],
        [NSString stringWithPRUnichars:checkMsg]);
  
  return NS_OK;
}


NS_IMETHODIMP 
CHMsgProgress::PromptPassword(const PRUnichar* dialogTitle, const PRUnichar* text, 
                              PRUnichar* *password, const PRUnichar* checkMsg, 
                              PRBool* checkValue, PRBool* _retval)
{
  NSLog(@"PromptPassword()");
  return NS_OK;
}

NS_IMETHODIMP 
CHMsgProgress::PromptUsernameAndPassword(const PRUnichar* dialogTitle, const PRUnichar* text,
                                         PRUnichar* *username, PRUnichar* *password, const PRUnichar* checkMsg, 
                                         PRBool* checkValue, PRBool* _retval)
{
  NSLog(@"PromptUsernameAndPassword()");
  return NS_OK;
}

NS_IMETHODIMP 
CHMsgProgress::Select(const PRUnichar* dialogTitle, const PRUnichar* text, PRUint32 count, 
                      const PRUnichar* *selectList, PRInt32* outSelection, PRBool* _retval)
{
  NSLog(@"Select()");
  return NS_OK;
}

